/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.common;

import hibernate.dao.AccountDAO;
import hibernate.dao.RoleDAO;

import hibernate.entity.Roles;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import util.Cookies;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class Login extends AbstractUIBean {

    /**
     * Creates a new instance of Login
     */
    String username;
    String password;
    Roles role;
    RoleDAO roleDao = new RoleDAO();
    AccountDAO accDao = new AccountDAO();
    boolean isRemember, isError;

    public List<Roles> getAllRole() {
        return roleDao.readAll();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Roles getRole() {
        return role;
    }

    public void setRole(Roles role) {
        this.role = role;
    }

    public boolean isIsRemember() {
        return isRemember;
    }

    public void setIsRemember(boolean isRemember) {
        this.isRemember = isRemember;
    }

    public boolean isIsError() {
        return isError;
    }

    public void setIsError(boolean isError) {
        this.isError = isError;
    }

    public void dologin() {
        hibernate.entity.Accounts lgn = accDao.login(username, password);
        if (lgn != null) {
            if (isRemember) {
                Cookies.add("cookLogin", String.valueOf(lgn.getAccountId()));
            }
            if (roleDao.isLoginHaveRole(lgn, role)) {
                try {
                    response.sendRedirect("profile");
                } catch (IOException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void forgot() {
    }

    public Login() {
    }
}
